# # 一、控制台输出日志
# import logging  # 导入logging模块
# # 日志级别默认是WARNING
# logging.basicConfig(level=logging.WARNING,
#           format="%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
# # 开始使用log功能
# logging.info("This is loggging info message")
# logging.debug("This is loggging debug message")
# logging.warning("This is loggging a warning message")
# logging.error("This is loggging error message")
# logging.critical("This is loggging critical message")

# # 二、日志，输出到文件
# import logging
# # 配置日志的输出文件，以及文件的打开方式
# logging.basicConfig(level=logging.WARNING,
#           filename="logs/log.txt",
#           filemode="a",  # a表示追加的模式打开文件
#           format="%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
# # use logging
# logging.info("This is loggging info message")
# logging.debug("This is loggging debug message")
# logging.warning("This is loggging a warning message")
# logging.error("This is loggging error message")
# logging.critical("This is loggging critical message")

# 三、日志，输出到文件，并且输出到控制台
import logging,time
# nowDate = time.strftime("%Y-%m-%d",time.localtime())
# print(nowDate)
# 第一步，创建一个logger
logger = logging.getLogger()
# logging 日志级别设置
logger.setLevel(logging.DEBUG)
# 第二步，创建一个handler，用于写入日志文件
logfile = "logs/log.txt"
# mode表示文件的打开方式，encoding用于设置编码格式防止写入中文乱码
fh = logging.FileHandler(logfile, mode="a",encoding="UTF-8")
# 设置输出到文件中的日志级别
fh.setLevel(logging.DEBUG)
# 第三步，再创建一个handler，用于输出到控制台
ch = logging.StreamHandler()
# 设置输出到console控制台中的日志级别
ch.setLevel(logging.INFO)
# 第四步，定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 第五步，将logger添加到handler里面
logger.addHandler(fh)
logger.addHandler(ch)

# # 日志内容测试一
# value = 123
# logger.debug("This is logger debug message")
# logger.info("This is logger info message")
# logger.warning("This is logger warning message")
# logger.error("This is logger error message")
# logger.critical("This is logger critical message")
# logger.critical("info获得当前路径:{}".format(value))

# # 日志内容测试二
# if __name__ == "__main__":
#     try:
#         logger.info("info message：输出 m =1/0")
#         m = 1/0
#     except Exception as e:
#         logger.error("error message")
